//
//  QForgetPassWordVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/4.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QForgetPassWordVC.h"
#import "QForgetPassWordCell.h"

@interface QForgetPassWordVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tbView;

@end

@implementation QForgetPassWordVC{

    NSString *_phoneStr;
    NSString *_codeStr;
    NSString *_passWordStr;
    
    BOOL oneBool,twoBool,threeBool;
    
    UIButton *_agreeButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupForDismissKeyboard];//释放键盘
    [self createUI];
    
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(forgetBack)];

}

- (void)forgetBack{

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createUI{

    WS(wSelf);
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(wSelf.view).insets(UIEdgeInsetsZero);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QForgetPassWordCell" bundle:nil] forCellReuseIdentifier:@"QForgetPassWordCell"];
    
    _agreeButton = [QViewUtility createButtonWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50) font:17 title:@"确定" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [_agreeButton blueStyle];
    [_agreeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_agreeButton];
}

- (void)buttonAction:(UIButton *)button{
    if(oneBool && twoBool && threeBool){
        [self requestData];
    }else{
        if(!oneBool){
            if(_phoneStr.length == 0){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
            
        }else if (!twoBool){
            if(_codeStr.length == 0){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写验证码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确验证码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
        }else if (!threeBool){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入新密码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }
}

#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
    }
    return _tbView;
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QForgetPassWordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QForgetPassWordCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __block QForgetPassWordVC *wself = self;
    cell.block = ^(UITextField *textField){
        [wself textFieldAction:textField];
    };
    cell.codeBlock = ^(UIButton *button){
        if(_phoneStr.length == 11){
            [QParamService onTimer:button type:NO];
            [wself requestAuthCodel];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确手机号" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    };
    [cell setupAttbuild:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    if(section == 0){
        return 0.01;
    }else{
        return 100;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

- (void)textFieldAction:(UITextField *)textField{
    if(textField.tag == 1){
        _phoneStr = textField.text;
        if(_phoneStr.length == 11){
            oneBool = YES;
        }else{
            oneBool = NO;
        }
    }else if (textField.tag == 2){
        _codeStr = textField.text;
        if(_codeStr.length == 6){
            twoBool = YES;
        }else{
            twoBool = NO;
        }
    }else if (textField.tag == 3){
        _passWordStr = textField.text;
        if(_passWordStr.length > 0){
            threeBool = YES;
        }else{
            threeBool = NO;
        }
    }
}

#pragma mark-----请求验证码接口
- (void)requestAuthCodel{
    NSString *url = [QParamService urlWithAction:@"getVerificationCode"];
    NSDictionary *param = [QParamService getauthcodetel:_phoneStr type:@"2"];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"验证码已发送,请注意查收" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark-----修改密码
- (void)requestData{

    NSString *url = [QParamService urlWithAction:@"forgetPassword"];
    NSTimeInterval inter = [[NSDate date] timeIntervalSince1970] * 1000;//当前时间戳
    NSString *machineCode = [NSString stringWithFormat:@"ios_%.f",inter];
    
    NSString *md5Str = [QMD5Hash md5String:_passWordStr];
    md5Str = [md5Str uppercaseString];
    
    NSDictionary *param = [QParamService forgetPassWordWithtel:_phoneStr verificationCode:_codeStr password:md5Str machineCode:machineCode];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"修改成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"修改失败" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
        }
    }];
}

@end
